package br.com.android.watcher;

import java.util.ArrayList;
import java.util.Collections;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TwoLineListItem;

public class ToonAdapter extends BaseAdapter {
	ArrayList<Toon> list;
	Context ctx;
	
	public ToonAdapter(Context ctx){
		this.list = new ArrayList<Toon>();
		this.ctx = ctx;
	}
	@Override
	public int getCount() {
		return this.list.size();
	}

	@Override
	public Toon getItem(int position) {
		return this.list.get(position);

	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//Log.v("Watcher Act", "Executando o metodo getView");
		
		TwoLineListItem viewItem;
		Toon t = this.list.get(position);
		if (convertView == null) {
			viewItem = (TwoLineListItem) LayoutInflater.from(this.ctx).inflate(R.layout.listitem, parent, false);
		} else {
			viewItem = (TwoLineListItem) convertView;
		}
		viewItem.getText1().setText("(" + t.getScore() + ") " + t.getName());
		viewItem.getText2().setText(t.getSocialInfo());
		return viewItem;
	}
	public ArrayList<Toon> getAllToons(){
		return this.list;
	}
	public void add(Toon t){
		add(t,list.size());
		return;
	}
	public void remove(int pos) {
		this.list.remove(pos);
		notifyDataSetChanged();
		return;
	}
	public void sort() {
		Collections.sort(this.list, new Toon());
		notifyDataSetChanged();
		return;
	}
	public void add(Toon t, int pos) {
		this.list.add(pos, t);
		notifyDataSetChanged();
		return;
	}

}
